package com.excel;

import com.excel.data.CheckExcelErrorType;
import com.excel.data.CheckExcelRowDTO;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

import static com.excel.data.CheckExcelErrorType.*;

/**
 * @author yutao
 * @since 2020/4/30 4:08 下午
 */
public class StockChecker {

    /**
     * 校验Excel数据
     */
    public void checkExcelData(List<CheckExcelRowDTO> checkExcelRowDTOList) {
        checkExcelRowDTOList.forEach(s -> {
            Boolean isRoom = s.getIsRoom();
            Integer storeId = s.getStoreId();
            String unitTypeName = s.getUnitTypeName();

            List<CheckExcelErrorType> result = Lists.newArrayList();
            if (storeId == null) {
                result.add(WMS_CLINIC_NUM_NOT_EXISTS);
            }
            if (StringUtils.isBlank(unitTypeName)) {
                result.add(WMS_UNIT_TYPE_NOT_EXISTS);
            }
            if (isRoom == null) {
                result.add(WMS_ROOM_NOT_EXISTS);
            }
            String batchNo = s.getBatchNo();
            Integer skuId = s.getSkuId();
            Integer checkCount = s.getCheckCount();
            if (StringUtils.isNotBlank(batchNo)) {
                if (checkCount == null) {
                    result.add(WMS_BATCH_NO_CHECK_COUNT_NOT_EXISTS);
                }
                if (skuId == null) {
                    result.add(WMS_SKUID_NOT_EXISTS);
                }
            }
            s.setErrorCellIds(result.stream().flatMap(error -> error.getErrorCellIndex()
                    .stream()).collect(Collectors.toSet()));
            s.setErrorMessage(result.stream().map(CheckExcelErrorType::getErrorMessage)
                    .distinct().collect(Collectors.joining("，")));
        });
    }

}
